编程的对话
将一个简单的“你好,世界!”程序与猜数字游戏之间的区别,看作是 独白 和 对话的区别。在独白中,程序发言后就退出;而在对话中,程序会提出问题、开启监听,并暂停其内部时钟,直到用户作出回应为止。
1. 前置预设与作用域
Rust 会自动将一组称为 前置预设 的项目导入到每个程序中。然而,对于终端输入等特定任务,我们必须显式地将 标准库 引入作用域,使用 use std::io;。这架起了程序内部逻辑与外部环境之间的桥梁。
2. 宏与函数的区别
你会注意到 println! 以感叹号结尾。这表明它是一个 宏。与普通函数不同,宏可以处理可变数量的参数,并在编译时执行字符串插值(填充 {guess})
3. 交互生命周期
当你运行 cargo run时,程序初始化,到达 io::stdin().read_line(),并暂停。它等待用户按下回车键,然后将该输入打包为一个 Result 类型,以安全地处理潜在的硬件故障。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the 'prelude' in Rust?
A list of all external crates installed via Cargo.
A small set of items automatically imported into every program.
A special function that runs before main().
The documentation generated by rustdoc.
✅ Correct!
Correct! The prelude includes common types like Option and Result so you don't have to 'use' them manually every time.❌ Incorrect
The prelude is about automatic imports, not external crates or specific execution timing.QUESTION 2
Why does
println! have an exclamation mark?It indicates a high-priority operation.
It is a macro, not a function.
It signals that the operation might crash.
It is an async function.
✅ Correct!
Exactly! In Rust, the '!' syntax distinguishes macros, which expand into more code before the program is compiled.❌ Incorrect
The '!' specifically denotes a macro in Rust syntax.QUESTION 3
Which line brings the I/O library into scope?
import std.io;use std::io;include io;load std::io;✅ Correct!
Rust uses the use keyword to bring modules or items into the current scope.❌ Incorrect
Unlike Java or Python, Rust uses use and double colons (::) for path resolution.QUESTION 4
What does
String::new() create?A static string slice.
A new, empty instance of a growable String.
A pointer to a character array.
An encrypted string buffer.
✅ Correct!
String::new() creates an empty, growable UTF-8 encoded string on the heap.❌ Incorrect
This creates a dynamic String object, not a static literal.QUESTION 5
What is the purpose of the
.expect() method in the code?To wait for 5 seconds before failing.
To handle the potential error of the
Result type.To format the output string.
To validate that the input is a number.
✅ Correct!
read_line returns a Result. .expect() will crash the program with a message if that result is an error.❌ Incorrect
expect is a simple (though aggressive) form of error handling for Result types.Module: The Input/Output Bridge
Analyzing the Interactive Flow
A developer writes a CLI tool but forgets the 'mut' keyword in 'let mut guess = String::new();'. They then attempt to pass '&guess' to 'read_line'.
Q
1. Why will the compiler reject passing '&guess' to the 'read_line' method?
Solution:
The 'read_line' method requires a mutable reference ('&mut String') to modify the string with user input. If the variable isn't declared with 'mut', we cannot create a mutable reference to it.
The 'read_line' method requires a mutable reference ('&mut String') to modify the string with user input. If the variable isn't declared with 'mut', we cannot create a mutable reference to it.
Q
2. Explain the difference between 'std::io' and the 'prelude' in the context of this game.
Solution:
The prelude contains basic items (like 'String') used in almost every program, while 'std::io' is a module in the standard library that provides functionality for input/output which must be explicitly brought into scope.
The prelude contains basic items (like 'String') used in almost every program, while 'std::io' is a module in the standard library that provides functionality for input/output which must be explicitly brought into scope.
Q
3. What terminal command compiles and executes the code in one step?
Solution:
The command is 'cargo run'. It checks for changes, compiles if necessary, and then launches the resulting binary immediately.
The command is 'cargo run'. It checks for changes, compiles if necessary, and then launches the resulting binary immediately.